package com.weijun.keepalive.service

import android.app.Service
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import com.weijun.keepalive.ACTION_SCREEN_OFF
import com.weijun.keepalive.ACTION_SCREEN_ON
import com.weijun.keepalive.CLICK_NOTIFICATION
import com.weijun.keepalive.NOTIFY_LEVEL
import com.weijun.keepalive.isRunningTaskExist
import com.weijun.keepalive.isScreenOn
import com.weijun.keepalive.logE
import com.weijun.keepalive.receiver.NotificationClickReceiver
import com.weijun.keepalive.utils.createNotification

/**
 * @author: weijun
 * date: 2023/7/6
 * description: 远程守护进程
 */
class RemoteService : Service() {

    private var bindRemoteServiceFlag = false
    private lateinit var mBinder:MyBinder

    override fun onCreate() {
        super.onCreate()
        "RemoteService onCreate".logE()
        if (::mBinder.isInitialized.not()) {
            mBinder = MyBinder()
        }
    }

    override fun onBind(intent: Intent?): IBinder = mBinder

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        "RemoteService onStartCommand".logE()
        try {
            bindRemoteServiceFlag = this.bindService(Intent(this@RemoteService, LocalService::class.java), remoteConnection, BIND_ABOVE_CLIENT)
        } catch (ignore: Exception) { }
        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        "RemoteService onDestroy".logE()
        try {
            if (bindRemoteServiceFlag) {
                unbindService(remoteConnection)
            }
        } catch (ignore: Exception) { }
    }

    private val remoteConnection = object :ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            "RemoteService onServiceConnected".logE()
        }
        override fun onServiceDisconnected(name: ComponentName?) {
            "RemoteService onServiceDisconnected".logE()
            whenServiceDisconnect()
        }
    }

    private fun whenServiceDisconnect() {
        if (applicationContext.isRunningTaskExist("$packageName:remote")) {
            startService(Intent(this, LocalService::class.java))
            bindRemoteServiceFlag = bindService(Intent(this, LocalService::class.java), remoteConnection, BIND_ABOVE_CLIENT)
        }
        if (isScreenOn()) {
            sendBroadcast(Intent(ACTION_SCREEN_ON))
        } else {
            sendBroadcast(Intent(ACTION_SCREEN_OFF))
        }
    }

    private inner class MyBinder : GuardAidl.Stub() {
        override fun wakeUp(title: String?, discription: String?, iconRes: Int) {
            "RemoteService wakeUp".logE()
            if (Build.VERSION.SDK_INT < 25) {
                this@RemoteService.startForeground(NOTIFY_LEVEL, createNotification(
                    Intent(applicationContext, NotificationClickReceiver::class.java).apply {
                        action = CLICK_NOTIFICATION
                    }
                ))
            }
        }
    }
}